package test.hxy.mongoSchoolSimple.dao;

import org.bson.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.BasicQuery;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Repository;
import test.hxy.mongoSchoolSimple.model.Book;

import java.util.List;

@Repository
public class BookTemplateDao {

    @Autowired
    MongoTemplate mongoTemplate;
    public void insert(Book book){
        mongoTemplate.insert(book);
    }
    public void delete(String isbn) {
        Query query = new BasicQuery(new Document("isbn", isbn));
        mongoTemplate.remove(query, Book.class);
    }
    public List<Book> findByteacherIdEquals(String teacherId) {
        Query query = new Query();
        query.addCriteria(Criteria.where("author").all(teacherId));
        return mongoTemplate.find(query, Book.class);
    }
}
